home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-10 | 28.0 KB | 1,056 lines | [TEXT/CWIE] |
- // =================================================================================
- // CDocument.cp ©1997 BB's Team inc. All rights reserved
- // =================================================================================
- #include "PL_Utils.h"
- #include "PLConstants.h"
- #include "LDynamicPopupMenu.h"
- #include "CDynamicEditField.h"
- #include "CMemoryIndicator.h"
- #include "CGreyCaption.h"
- #include "GWorldSaver.h"
- #include "CDocument.h"
-
- #include <LFile.h>
- #include <LPrintout.h>
- #include <LPlaceHolder.h>
- #include <LString.h>
- #include <LWindow.h>
- #include <LCaption.h>
- #include <LEditField.h>
- #include <PP_Messages.h>
- #include <PP_KeyCodes.h>
- #include <UMemoryMgr.h>
- #include <UWindows.h>
- #include <UReanimator.h>
- #include <UDrawingState.h>
- #include <UDesktop.h>
- #include <UPrintingMgr.h>
-
- #ifdef PL_PROFILE
- #include <UProfiler.h>
- #endif
-
-
- // ---------------------------------------------------------------------------------
- // • CDocument(LCommander*, FSSpec*, CPreferences*)
- // ---------------------------------------------------------------------------------
- CDocument::CDocument(
- LCommander *inSuper,
- FSSpec *inFileSpec,
- CPreferences *inPreferences,
- Boolean inVisible)
- : LSingleDoc (inSuper)
- , mPreferences (inPreferences)
- , mVisible(inVisible)
- {
- // Create the window and show it
- mWindow = LWindow::CreateWindow (rPPob_MainWindow, this);
- Assert_ (mWindow != nil);
- mWindow->SetDefaultSubModel (this); // MJS 97-05-29
- if (mVisible) mWindow->Show();
-
- // Keep easy access to important Panes
- mDynamicText = (CDynamicText*) mWindow -> FindPaneByID (kDynamicText);
- Assert_ (mDynamicText != nil);
-
- mShowPane = (CGWorldPane*) mWindow -> FindPaneByID (kShowPane);
- Assert_ (mShowPane != nil);
-
- //
- // If the prefs are read from the prefs file, we initialize with them.
- // if they're not, we initialize THEM with the default values set in
- // the interface definition (Constructor PPob).
- // We must set take care with buttons in radio groups, because only value
- // Button_On makes them broadcast to the radio group to set off the others.
-
- // text traits
- if ( mPreferences->IsOk() ) {
- mPreferences->GetTextTraits (mTextTraits);
- mDynamicText->SetTextTraits (mTextTraits);
- }
- else {
- mDynamicText->GetTextTraits (mTextTraits);
- mPreferences->SetTextTraits (mTextTraits);
- }
- mFontLight.SetTextTraits (mTextTraits);
- mImageLight.SetTextTraits (mTextTraits);
-
- // optimize for screen / printer
- if ( mPreferences->IsOk() )
- if ( mPreferences->GetScreen() )
- mWindow->SetValueForPaneID (kScreenButton, Button_On);
- else
- mWindow->SetValueForPaneID (kPrinterButton, Button_On);
- else
- mPreferences->SetScreen ( mWindow->GetValueForPaneID(kScreenButton) );
- mFontLight.SetScreen (mPreferences->GetScreen());
-
- // Ascii 7/8 bits
- if ( mPreferences->IsOk() )
- if ( mPreferences->Get7Bits() )
- mWindow->SetValueForPaneID (k7bitsButton, Button_On);
- else
- mWindow->SetValueForPaneID (k8bitsButton, Button_On);
- else
- mPreferences->Set7Bits ( mWindow->GetValueForPaneID (k7bitsButton) );
- mFontLight.Set7Bits (mPreferences->Get7Bits());
-
- // AutoContrast on/off
- if ( mPreferences->IsOk() )
- mWindow->SetValueForPaneID (kContrastCheck, mPreferences->GetContrast() );
- else
- mPreferences->SetContrast (mWindow->GetValueForPaneID(kContrastCheck) );
- mGenText.SetContrast (mPreferences->GetContrast());
-
- //
- // Initialize controls that are not in the window but in the prefs dialog
- // The prefs ctor provides values for these ones
- //
- // mMonoSpace global to all documents
- // mConfirmPrinting idem
- // mBeep32k idem
- mFontLight.SetAllowed ( mPreferences->GetAllowed() );
-
- // This is too dangerous to save in prefs.
- // We take it from the PPob
- // I'll add a "default zoom" field in the preferences… in another life.
- mRatio = mWindow->GetValueForPaneID (kRatioEditField);
-
- // Set the area used to compute the font lightness
- // We use the frame of the ShowPane… why not ?!
- Rect theFrame;
- mShowPane->CalcLocalFrameRect (theFrame);
- mFontLight.SetRect (theFrame);
-
- // we don't allow normal events processing while computing…
- mWindow->UpdatePort();
-
- // Initialize font and size popups
- SetupControls ();
-
- // NOW we can add self as listener to all controls in the Window
- // (no need for StopListening() and StartListening() to avoid unnecessary messages)
- UReanimator::LinkListenerToControls (this, mWindow, rRidL_MainWindow);
-
- // we don't allow normal events processing while computing…
- mWindow->UpdatePort();
-
- // Finally open the file
- if ( OpenFile (*inFileSpec) )
- SetModified (true);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ~CDocument
- // ---------------------------------------------------------------------------------
- CDocument::~CDocument()
- {}
-
-
- // ---------------------------------------------------------------------------------
- // • ~CDocument
- // ---------------------------------------------------------------------------------
- Boolean CDocument::UpdateZoom()
- {
- // LEditField *theField = (LEditField*) mWindow->FindPaneByID (kRatioEditField);
- CDynamicEditField *theField = (CDynamicEditField*) mWindow->FindPaneByID (kRatioEditField);
- Assert_ (theField != nil);
-
- Int32 theRatio;
- theRatio = theField->GetValue();
-
- if ( theRatio!=mRatio ) {
- mRatio=theRatio;
- ReadPictFile();
- }
- return true;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • HandleKeyPress
- // ---------------------------------------------------------------------------------
- Boolean CDocument::HandleKeyPress (const EventRecord &inKeyEvent)
- {
- Int16 theKey = inKeyEvent.message & charCodeMask;
-
- if ( theKey==char_Enter || theKey==char_Return)
- return CDocument::UpdateZoom ();
- else
- return LCommander::HandleKeyPress(inKeyEvent);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetupControls
- // ---------------------------------------------------------------------------------
- void CDocument::SetupControls (void)
- {
- // Avoid too many messages while building
- StopListening();
-
- #ifdef powerc
- SetStatus (nil);
- #endif
-
- //
- // Font popup
- //
-
- // Get the font popup.
- LDynamicPopupMenu *thePopup;
- thePopup = (LDynamicPopupMenu *) mWindow->FindPaneByID( kFontPopup );
- Assert_( thePopup != nil );
-
- Int16 theCount = ::CountMenuItems( thePopup->GetMacMenuH() );
- Str255 theMenuText;
-
- // Eliminate non-mono-space fonts if so the user desires
- if ( mPreferences->GetMonoSpace() )
- for ( Int16 i=theCount; i>0; --i ) {
-
- ::GetMenuItemText( thePopup->GetMacMenuH(), i, theMenuText );
-
- // update user info
- LStr255 theText (rSTR, kCheckingMonoSpace);
- theText.Append (theMenuText);
- SetStatus (theText);
-
- if ( ::CompareString( mTextTraits.fontName, theMenuText, nil ) == 0 ) {
- // this is the one we want to set the popup to, don't delete !
- }
- else if ( !PL_Utils::IsMonoSpace (theMenuText) )
- ::DeleteMenuItem (thePopup->GetMacMenuH(), i);
- }
-
- // Reset the popup value
- theCount = ::CountMenuItems( thePopup->GetMacMenuH() );
- for ( Int16 i=1 ; i<=theCount ; ++i ) {
- ::GetMenuItemText( thePopup->GetMacMenuH(), i, theMenuText );
- if ( ::CompareString( mTextTraits.fontName, theMenuText, nil ) == 0 ) {
- thePopup->FinishedCreatingMenu (i);
- break;
- }
- }
- thePopup->Refresh();
-
- //
- // Size popup
- //
-
- // Get the size popup.
- thePopup = (LDynamicPopupMenu *) mWindow->FindPaneByID ( kSizePopup );
- Assert_( thePopup != nil );
-
- // Get the number of items in the menu (-2 for the 'Other…' item)
- theCount = -2 + ::CountMenuItems( thePopup->GetMacMenuH() );
-
- // Set the popup value (assumes our size is in the popup !)
- for ( Int16 i=1; i<=theCount; ++i ) {
-
- // Get the menu item text.
- Str255 theMenuText;
- ::GetMenuItemText( thePopup->GetMacMenuH(), i, theMenuText );
-
- // Get the size referred to by the menu item.
- Int32 theSize;
- ::StringToNum( theMenuText, &theSize );
-
- if ( mTextTraits.size == theSize ) {
-
- // Set the value for the popup.
- thePopup->FinishedCreatingMenu (i);
- break;
-
- }
- }
-
- // Adjust the size menu styles (bold where IM says it should)
- AdjustSizeMenuForFont (mTextTraits.fontNumber);
-
- // Start listening again.
- StartListening();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetName
- // ---------------------------------------------------------------------------------
- void
- CDocument::SetName (Str63 inName)
- {
- LStr255 name (inName);
- LStr255 up (name);
- ::UppercaseText((Ptr) &up[1], up[0], smSystemScript);
- if (up.EndsWith ("PICT", 4) )
- name.Replace (name.Length()-3, 4, "txt", 3);
- else
- name.Append (".txt",4);
-
- mWindow->SetDescriptor (name);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • OpenFile
- // ---------------------------------------------------------------------------------
- Boolean
- CDocument::OpenFile(
- FSSpec &inFileSpec )
- {
- OSErr err;
-
- // Create a new file object & open the data fork.
- mFile = new LFile (inFileSpec);
-
- // Read the Pict, create an offscreen copy
- err = ReadPictFile();
-
- // Set window title acording to the name of the file.
- SetName (inFileSpec.name);
-
- // Flag that document has an associated file (not in this particular case !)
- mIsSpecified = false;
-
- return (err==noErr);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ReadPictFile
- // ---------------------------------------------------------------------------------
- OSErr CDocument::ReadPictFile (void)
- {
- OSErr err;
-
- SetStatus (rSTR, kReadingPictFile);
-
- // Ask for room : they will change anyway
- mImageLight.MakeRoom (true); // light buffer : 1 float per text char
- mGenText.MakeRoom (); // text buffer : 1 char per char
- SetTextStr ("\p"); // TE record : idem, limited to 32k
- mGreyPict.MakeRoom (); // offscreen grey picture
-
- Int16 theDataFork;
- theDataFork = mFile->OpenDataFork (fsRdWrPerm);
-
- Try_ {
-
- // Read the Pict file into a Handle
- Int32 fileLength;
- err = ::GetEOF(theDataFork, &fileLength);
- ThrowIfOSErr_(err);
-
- fileLength -= kPICTHeaderSize;
-
- // throws if mem fails.
- // uses temp mem if not enough in core
- // dtor is called if an exception is thrown
- StHandleBlock thePict (fileLength, true, true);
-
- err = ::SetFPos(theDataFork, fsFromStart, kPICTHeaderSize);
- ThrowIfOSErr_(err);
-
- err = ::FSRead(theDataFork, &fileLength, *(Handle)thePict);
- ThrowIfOSErr_(err);
-
- mFile->CloseDataFork();
-
- // Finish creation of mGrey
- Rect offRect;
- offRect = (**(PicHandle)(Handle)thePict).picFrame; // (Handle) accessor, not PicHandle…
- ::OffsetRect (&offRect, -offRect.left, -offRect.top);
- offRect.right *= mRatio/100.;
- offRect.bottom *= mRatio/100.;
-
- // the real allocation happens here
- mGreyPict.SetRect (offRect);
-
- // Copy the picture to offscreen
- if ( mGreyPict.IsOK() ) {
- GWorldSaver saveGW (mGreyPict);
- mGreyPict.Lock();
- ::EraseRect ( &offRect );
- StColorState::Normalize();
- ::DrawPicture ((PicHandle)(Handle)thePict, &offRect);
- mGreyPict.Unlock();
- }
-
- // dtor does ::DisposeHandle ((Handle)thePict);
- }
-
- Catch_ (inErr) {
- } EndCatch_
-
- // erase message bar
- SetStatus();
-
- // Tell'em we changed
- mShowPane->SetGWorld (mGreyPict);
- mImageLight.SetGreyWorld (&mGreyPict);
- SetModified (true);
- Compute();
-
- return err;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoAESave
- // ---------------------------------------------------------------------------------
- void
- CDocument::DoAESave(
- FSSpec &inFileSpec,
- OSType inFileType )
- {
- // Delete the existing file object.
- delete mFile;
-
- // Make a new file object.
- mFile = new LFile (inFileSpec);
-
- // Get the proper file type.
- OSType theFileType = 'TEXT';
- if (inFileType !=fileType_Default)
- theFileType = inFileType;
-
- // Make new file on disk
- mFile->CreateNewDataFile ('CWIE', theFileType);
-
- // Write out the data.
- DoSave();
-
- // Change window title to reflect the new name.
- mWindow->SetDescriptor (inFileSpec.name);
-
- // Document now has a specified file.
- mIsSpecified = true;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------------
- void
- CDocument::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName )
- {
- switch ( inCommand ) {
-
- default:
- {
- // Call inherited.
- LSingleDoc::FindCommandStatus( inCommand,
- outEnabled, outUsesMark, outMark, outName );
- }
- break;
-
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- // ---------------------------------------------------------------------------
- Boolean CDocument::ObeyCommand (
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
-
- switch (inCommand) {
-
- case cmd_PageSetup:
- SetupPage();
- break;
-
- case cmd_LooseFocus:
- UpdateZoom();
- break;
-
- case cmd_Preferences:
- LStr255 allowed ( mFontLight.GetAllowed() );
- mPreferences->DoDialog (allowed, mTextTraits.fontName);
- if ( allowed != mPreferences->GetAllowed() ) {
- mFontLight.SetAllowed ( mPreferences->GetAllowed() );
- Compute ();
- }
- break;
-
- default:
- cmdHandled = LSingleDoc::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • AdjustSizeMenuForFont
- // ---------------------------------------------------------------------------------
- void
- CDocument::AdjustSizeMenuForFont (Int16 inFontNumber)
- {
- // Get the size popup.
- LDynamicPopupMenu *thePopup;
- thePopup = (LDynamicPopupMenu *) mWindow->FindPaneByID( kSizePopup );
- Assert_( thePopup != nil );
-
- // Get the number of items in the menu.
- Int16 theCount = ::CountMenuItems( thePopup->GetMacMenuH() );
-
- for ( Int16 i=1; i<=theCount -2 /* Other... */ ; ++i ) {
-
- // Get the menu item text.
- Str255 theMenuText;
- ::GetMenuItemText( thePopup->GetMacMenuH(), i, theMenuText );
-
- // Get the size referred to by the menu item.
- Int32 theSize;
- ::StringToNum( theMenuText, &theSize );
-
- // Set the item style.
- ::SetItemStyle( thePopup->GetMacMenuH(), i,
- ::RealFont( inFontNumber, theSize ) ? bold : normal );
- }
-
- // Refresh the popup.
- thePopup->Refresh();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoSave
- // ---------------------------------------------------------------------------------
- void
- CDocument::DoSave()
- {
- // Open the data fork.
- mFile->OpenDataFork (fsRdWrPerm);
-
- // the GenText may contain a nil Text. A user should not ask
- // for saving in such circumstances, but you never know...
- Handle theTextH;
-
- theTextH = mGenText.GetText();
- if ( theTextH==nil )
- theTextH = mDynamicText->GetTextHandle();
-
- // Lock the text handle (locally)
- StHandleLocker theLock (theTextH);
-
- // Write the text to the file.
- mFile->WriteDataFork (*theTextH, ::GetHandleSize (theTextH));
-
- // Close the data fork.
- mFile->CloseDataFork();
-
- // Saving makes doc un-dirty.
- SetModified (false);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ListenToMessage
- // ---------------------------------------------------------------------------------
- void
- CDocument::ListenToMessage (MessageT inMessage, void *ioParam)
- {
-
- UpdateZoom();
-
- switch ( inMessage ) {
-
- case msg_FontPopup:
- {
- // Get the font popup.
- LDynamicPopupMenu *thePopup;
- thePopup = (LDynamicPopupMenu *) mWindow->FindPaneByID ( kFontPopup );
- Assert_( thePopup != nil );
-
- // Get the menu item text. The value of the popup
- // is passed in ioParam (it's a long integer).
- ::GetMenuItemText( thePopup->GetMacMenuH(),
- *(Int32 *)ioParam, mTextTraits.fontName );
-
- // Get the font number.
- ::GetFNum( mTextTraits.fontName, &mTextTraits.fontNumber );
-
- // Adjust the size menu styles.
- AdjustSizeMenuForFont ( mTextTraits.fontNumber );
-
- // compute text with the new font and refresh view
- mFontLight.SetTextTraits (mTextTraits);
- mImageLight.SetTextTraits (mTextTraits);
- Compute();
- mPreferences->SetTextTraits (mTextTraits);
- }
- break;
-
- case msg_SizePopup:
- {
- // Get the size popup.
- LDynamicPopupMenu *thePopup;
- thePopup = (LDynamicPopupMenu *) mWindow->FindPaneByID( kSizePopup );
- Assert_( thePopup != nil );
-
- // Get the menu item text. The value of the popup
- // is passed in ioParam (it's a long integer).
- Str255 theMenuText;
- ::GetMenuItemText( thePopup->GetMacMenuH(),
- *(Int32 *)ioParam, theMenuText );
-
- // Get the size referred to by the menu item.
- Int32 theSize;
- ::StringToNum( theMenuText, &theSize );
-
- // Set the size in the text traits.
- mTextTraits.size = theSize;
-
- // compute text with the new font and refresh view
- mFontLight.SetTextTraits (mTextTraits);
- mImageLight.SetTextTraits (mTextTraits);
- Compute();
- mPreferences->SetTextTraits (mTextTraits);
- }
- break;
-
- case msg_ContrastCheck:
- {
- mGenText.SetContrast (*(Int32 *)ioParam==Button_On);
- Compute();
- mPreferences->SetContrast (*(Int32 *)ioParam==Button_On);
- }
- break;
-
- case msg_ControlClicked:
- {
- PaneIDT thePaneID = ((LPane*)ioParam)->GetPaneID();
- switch ( thePaneID) {
- case kScreenButton :
- case kPrinterButton :
- mFontLight.SetScreen ( thePaneID==kScreenButton );
- mPreferences->SetScreen ( thePaneID==kScreenButton );
- break;
- case k7bitsButton :
- case k8bitsButton :
- mFontLight.Set7Bits ( thePaneID==k7bitsButton );
- mPreferences->Set7Bits ( thePaneID==k7bitsButton );
- break;
- default :
- SysBeep (1);
- }
- Compute();
- }
- break;
-
- default:
- {
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Compute
- // ---------------------------------------------------------------------------------
- void
- CDocument::Compute (void)
- {
-
- #ifdef PL_PROFILE
- // StProfileSection profile ("\pProfile", 4000, 50);
- #endif
-
- // Reset the col and row info
- ClearInfo();
-
- // Reading Pict failed
- if ( !mGreyPict.IsOK() ) {
- SetTextStr (rSTR, kNoMemForPict);
- return;
- }
-
- // Update the FontLight
- if (!mFontLight.IsUpToDate() ) {
- mGenText.MakeRoom();
- SetStatus (rSTR, kComputingFontLight);
- mFontLight.Update ();
- SetStatus ();
- }
-
- // Still not UpToDate if a pb occured
- if (!mFontLight.IsUpToDate() ) {
- SetTextStr (rSTR, kNoMemForFont);
- return;
- }
-
- // Update the col and row info
- UpdateInfo();
-
- // if the 32k limit is exceeded, tell the user
- if ( mImageLight.GetTextSize() > 32000 )
- if ( ! mPreferences->GetBeep32k() )
- mPreferences->SetBeep32k (! TellOverflow());
- else
- SysBeep(1);
-
- // Update the ImageLight
- if (!mImageLight.IsUpToDate() ) {
- mGenText.MakeRoom();
- SetStatus (rSTR, kComputingImageLight);
- mImageLight.Update ();
- SetStatus ();
- }
-
- // Check we can go on
- if ( mImageLight.GetLight()==nil ) {
- SetTextStr (rSTR, kNoMemForPictLight);
- return;
- }
-
- // Update the GenText
- SetStatus (rSTR, kComputingText);
- mGenText.Update (mFontLight, mImageLight);
- SetStatus ();
- if (mGenText.GetText()==nil) {
- SetTextStr (rSTR, kNoMemForText);
- return;
- }
- SetModified (true);
-
- // Warning : do not invert SetWidth and SetTextPtr. I spent 2 hours on this one...
- mDynamicText->SetTextTraits (mTextTraits);
-
- // Why 40 ? Better for some non-monospace fonts… but not enough
- // for some other. Ought to be 1 or 2 if everything went fine.
- mDynamicText->SetWidth ( mImageLight.GetPictWidth() + 40 );
-
- Handle theText = mGenText.GetText();
- StHandleLocker theLock(theText);
-
- Int32 theSize;
- theSize = mImageLight.GetTextSize();
- if (theSize > 32000 )
- theSize = 32000;
-
- mDynamicText->SetTextPtr ( (Ptr)(*theText), theSize );
-
- // Update the number of pages
- UpdatePagesNumber();
- }
-
-
-
- // ---------------------------------------------------------------------------------
- // • UpdateInfo
- // ---------------------------------------------------------------------------------
- void CDocument::ClearInfo (void)
- {
- mWindow->SetDescriptorForPaneID( kLinesCaption , "\p" );
- mWindow->SetDescriptorForPaneID( kColumnsCaption, "\p" );
- mWindow->SetDescriptorForPaneID( kPagesCaption , "\p" );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • UpdateInfo
- // ---------------------------------------------------------------------------------
- void CDocument::UpdateInfo (void)
- {
- // Tell the size in line && column units
- LStr255 theLStr;
- Str255 theStr;
-
- theLStr = mImageLight.GetHeightN();
- GetIndString (theStr, rSTR, kLines);
- theLStr += theStr;
- mWindow->SetDescriptorForPaneID( kLinesCaption, theLStr );
-
- theLStr = mImageLight.GetWidthN();
- GetIndString (theStr, rSTR, kColumns);
- theLStr += theStr;
- mWindow->SetDescriptorForPaneID( kColumnsCaption, theLStr );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • UpdatePagesNumber
- // ---------------------------------------------------------------------------------
- void CDocument::UpdatePagesNumber (void)
- {
- // Tell the size in pages, between parenthesis if partial (>32k)
- LStr255 theLStr;
- Str255 theStr;
- Int32 thePages;
-
- thePages = DoMaybePrint (false);
- if (thePages>0) {
- theLStr = thePages;
- GetIndString (theStr, rSTR, kPages);
- theLStr += theStr;
- if (thePages>1)
- theLStr += "\ps";
- if ( mImageLight.GetTextSize() > 32000 )
- theLStr = "\p(" + theLStr + "\p)";
- mWindow->SetDescriptorForPaneID( kPagesCaption, theLStr );
- }
- }
-
-
-
- // ---------------------------------------------------------------------------------
- // • TellOverflow
- // ---------------------------------------------------------------------------------
- Boolean CDocument::TellOverflow (void)
- {
- UDesktop::Deactivate();
- Int16 answer = ::CautionAlert(ALRT_TellOverflow, nil);
- UDesktop::Activate();
- return answer==kButtonOk;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetStatus
- // ---------------------------------------------------------------------------------
- void CDocument::SetStatus (
- ConstStringPtr inDescriptor)
- {
- // Update the status line
- CGreyCaption *theCaption;
- theCaption = (CGreyCaption*) mWindow -> FindPaneByID (kGreyCaption);
- Assert_ (theCaption != nil);
-
- if (inDescriptor != nil)
- theCaption->SetDescriptor (inDescriptor); // (auto refresh)
-
- // Also update the memory indicator (no event loop at startup…)
- CMemoryIndicator *theMemo;
- theMemo = (CMemoryIndicator*) mWindow -> FindPaneByID (kMemoryIndicator);
- Assert_ (theMemo != nil);
- theMemo->SpendTime(); // (auto refresh)
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetStatus
- // ---------------------------------------------------------------------------------
- void CDocument::SetStatus (
- ResIDT inSTR,
- Int16 inIndex)
- {
- LStr255 theMessage (inSTR, inIndex);
- SetStatus (theMessage);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetTextStr
- // ---------------------------------------------------------------------------------
- void CDocument::SetTextStr (
- ConstStringPtr inMsg)
- {
- TextTraitsRecord theTextTraits;
- UTextTraits::LoadSystemTraits (theTextTraits);
- mDynamicText->SetTextTraits (theTextTraits);
- mDynamicText->SetTextPtr ((Ptr)&inMsg[1], inMsg[0]);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetTextStr
- // ---------------------------------------------------------------------------------
- void CDocument::SetTextStr (
- ResIDT inSTR,
- Int16 inIndex)
- {
- Str255 theText;
- GetIndString (theText, inSTR, inIndex);
- SetTextStr (theText);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetupPage
- // ---------------------------------------------------------------------------------
- void CDocument::SetupPage()
- {
- UDesktop::Deactivate();
-
- if (!mPrintRecordH)
- mPrintRecordH = UPrintingMgr::CreatePrintRecord();
-
- if (mPrintRecordH)
- UPrintingMgr::AskPageSetup (mPrintRecordH);
-
- UDesktop::Activate();
-
- // the number of pages might have changed
- UpdatePagesNumber();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoPrint
- // ---------------------------------------------------------------------------------
- void CDocument::DoPrint()
- {
- DoMaybePrint (true);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoMaybePrint
- // ---------------------------------------------------------------------------------
- Int16 CDocument::DoMaybePrint (Boolean doIt)
- {
- // Create the printout.
- LPrintout *thePrintout = LPrintout::CreatePrintout (rPPob_Printout);
- if (thePrintout==nil)
- return -1;
-
- // Set the print record.
- if (mPrintRecordH)
- thePrintout->SetPrintRecord (mPrintRecordH);
-
- // Get the text PlaceHolder.
- LPlaceHolder *thePlaceHolder = (LPlaceHolder*) thePrintout->FindPaneByID (kPlaceHolder);
- Assert_ (thePlaceHolder!=nil);
-
- // Save the text frame, and set it to the text image
- // PlaceHolders are based on frames dimensions, we need images dimensions
- // and PlaceHolder::InstallOccupant is not virtual...
- mDynamicText->PrepareToPrint();
-
- // Center only if the text fits on one page
- SDimension16 thePlaceSize, theTextSize;
- Int16 theAlignment = kAlignAbsoluteCenter;
-
- thePlaceHolder->GetFrameSize (thePlaceSize);
- mDynamicText ->GetFrameSize (theTextSize);
- if ( thePlaceSize.width < theTextSize.width
- || thePlaceSize.height < theTextSize.height )
- theAlignment = kAlignNone;
-
- // Install the text view in the PlaceHolder
- thePlaceHolder->InstallOccupant (mDynamicText, theAlignment);
-
- // Count pages
- UInt32 HPage, VPage;
- thePrintout->CountPanels (HPage, VPage);
-
- if ( HPage*VPage > 1 ) {
-
- // Synchronize the PlaceHolder with the characters dimensions.
- // (The CDocument knows we are in fixed width, not the CDynamicText)
- SetPrintFrameSize (thePlaceHolder);
-
- // ask confirmation if needed
- if ( doIt && mPreferences->GetPrintConfirm() ) {
-
- LStr255 theStr;
- theStr = (Int32)(HPage*VPage);
-
- ParamText (theStr, "\p", "\p", "\p");
- UDesktop::Deactivate();
- Int16 answer = ::CautionAlert(ALRT_ConfirmPrinting, nil);
- UDesktop::Activate();
-
- if ( answer==kButtonCancel )
- doIt = false;
- }
- }
-
- // Print.
- if (doIt)
- thePrintout->DoPrintJob();
-
- // Delete the printout.
- delete thePrintout;
- mDynamicText->RevertFromPrint();
-
- return HPage*VPage;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetPrintFrameSize
- // ---------------------------------------------------------------------------------
- void
- CDocument::SetPrintFrameSize (LPlaceHolder* inPlace)
- {
- // Get the frame size.
- SDimension16 theFrameSize;
- inPlace->GetFrameSize (theFrameSize);
-
- // Get the text edit record handle.
- TEHandle theTextEditH = mDynamicText->GetMacTEH();
-
- // Calculate the number of lines per page.
- Int16 theLinesPerPage;
- theLinesPerPage = theFrameSize.height / (**theTextEditH).lineHeight;
-
- // Calculate the number of chars per line
- Int16 theCharsPerLine;
- theCharsPerLine = theFrameSize.width/mImageLight.GetWidth();
-
- // Resize the frame to an integral number of lines/chars.
- inPlace->ResizeFrameTo (
- mImageLight.GetWidth() * theCharsPerLine+1,
- (**theTextEditH).lineHeight * theLinesPerPage,
- false );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Additional AppleScript support (Michael Schuerig 97-05-29)
- // ---------------------------------------------------------------------------------
-
- // ---------------------------------------------------------------------------------
- // • GetAEProperty
- // ---------------------------------------------------------------------------------
-
- void
- CDocument::GetAEProperty(
- DescType inProperty,
- const AEDesc &inRequestedType,
- AEDesc &outPropertyDesc) const
- {
- switch (inProperty) {
-
- case pContents: {
- Handle theTextH = (((CDocument*)this)->mGenText).GetText();
- if ( theTextH==nil )
- theTextH = mDynamicText->GetTextHandle();
-
- StHandleLocker theLock(theTextH);
- ThrowIfOSErr_( ::AECreateDesc(typeChar, *theTextH, ::GetHandleSize(theTextH), &outPropertyDesc) );
-
- break;
- }
- default:
- LSingleDoc::GetAEProperty(inProperty, inRequestedType,
- outPropertyDesc);
- break;
- }
- }
-
-
-
- void
- CDocument::AttemptClose(
- Boolean inRecordIt)
- {
- if (mVisible)
- LSingleDoc::AttemptClose(inRecordIt);
- else
- Close();
- }
-